package com.example.permission.myjbm.activitys;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.example.permission.myjbm.dao.BaseAty;
import com.example.permission.myjbm.net.ReleaseNet;
import com.jbm.jbmsupplier.util.GeoInfoEntity;
import com.jbm.jbmsupplier.util.IGeoEntityCtrl;
import com.jbm.jbmsupplier.util.IGeoinfo;
import com.jbm.jbmsupplier.util.IGeoinfoCtrl;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择地址页面
 */
public class DSYSelectAddress extends BaseAty implements View.OnClickListener{
private Spinner address_sp1,address_sp2;
    private ReleaseNet net;
    private NetWork netWork;
    private IGeoinfo ids;
    private   IGeoinfo[] addresslist;
    private ListView address_lv;
    private  List<GeoInfoEntity> date;
    private MyAddressAdapter lv_adapter;
    private StringBuffer buff;
    private ImageView lv_default;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
          switch(msg.what){
              case ReleaseNet.ADDRESSERROR:
                  IGeoEntityCtrl obj = (IGeoEntityCtrl) msg.obj;
                  date= obj.geoList;
                for(int i=0;i<date.size();i++){
                    date.get(i).setCloseId(0);
                }
                  if(date.size()==0){
                      lv_default.setVisibility(View.VISIBLE);
                  }else{
                      lv_default.setVisibility(View.GONE);
                  }
                  lv_adapter.setNotify(date);
                  break;
              case ReleaseNet.ADDRESSERROR1:
                  IGeoinfoCtrl obj1 = (IGeoinfoCtrl) msg.obj;
                  if (obj1.back){
                  addresslist = obj1.GIlist;
                  List<String> list=new ArrayList<String>();
                  for(int i=0;i<addresslist.length;i++)
                  {
                      list.add(addresslist[i].gi_name);
                  }
                      ArrayAdapter<String> adapter = new ArrayAdapter<String>(DSYSelectAddress.this, android.R.layout.simple_spinner_item, list);
                      //第三步：为适配器设置下拉列表下拉时的菜单样式。
                      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                      //第四步：将适配器添加到下拉列表上
                      address_sp2.setAdapter(adapter);
                      address_sp2.setSelection(4);
              }
                  break;

          }
            loader.dismiss();
        }
    };
    private DialogLoading loader;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dsy_aty_select_address);
        net=new ReleaseNet(handler);

        initView();
        loader.show();
        net.getAddress1(-1);

    }

    private void initView() {
        loader=new DialogLoading(this);
        buff=new StringBuffer();
        address_sp1=(Spinner)findViewById(R.id.address_sp1);
        address_sp2=(Spinner)findViewById(R.id.address_sp2);
        address_lv=(ListView)findViewById(R.id.address_lv);
        TextView  title=(TextView)findViewById(R.id.header_title);
        TextView  right=(TextView)findViewById(R.id.header_right);
        ImageView  back=(ImageView)findViewById(R.id.header_back);
        lv_default=(ImageView)findViewById(R.id.lv_default);

        title.setText("选择地址");
        right.setText("确定");
        back.setOnClickListener(this);
        right.setOnClickListener(this);

        date=new ArrayList<>();
        lv_adapter=new MyAddressAdapter(date,this);
        address_lv.setAdapter(lv_adapter);

        ArrayList list=new ArrayList();
        list.add("天津市");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        //第三步：为适配器设置下拉列表下拉时的菜单样式。
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //第四步：将适配器添加到下拉列表上
        address_sp1.setAdapter(adapter);

        address_sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                loader.show();
                buff.delete(0,buff.length());
                buff.append("天津市 "+addresslist[position].gi_name+" ");
                net.getAddress(0,addresslist[position].gi_id,"village");
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.header_back:
                DSYSelectAddress.this.finish();

                break;
            case R.id.header_right:
                Intent intent=new Intent();
                    intent.putExtra("address",lv_adapter.getAddressName());
                intent.putStringArrayListExtra("data", (ArrayList<String>) lv_adapter.getBackAddress());
                setResult(RESULT_OK,intent);
                finish();
                break;
        }


    }

    class MyAddressAdapter extends BaseAdapter{
        private List<GeoInfoEntity> date;
        private List<String> scopeList;

       private Context c;

        public MyAddressAdapter( List<GeoInfoEntity> date,Context c){
            this.date=date;
            this.c=c;
             scopeList=new ArrayList<>();

        }
        public void setNotify(List<GeoInfoEntity> date){
            this.date=date;
            notifyDataSetChanged();
        }


        @Override
        public int getCount() {
            return date.size();
        }

        @Override
        public Object getItem(int position) {
            return date.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyHolder holder=null;
            if(convertView==null) {
              convertView = View.inflate(c, R.layout.dsy_item_address, null);
               holder = new MyHolder(convertView);
                convertView.setTag(holder);
          }else{
               holder= (MyHolder)convertView.getTag();

            }
            adapterInitView(date.get(position),holder);
            return convertView;
        }
        //需要传入接口
        public  List<String> getBackAddress(){
                for(int i=0;i<date.size();i++){
                    if(date.get(i).getCloseId()==1){
                        scopeList.add(date.get(i).getGiId()+"");
                    }
                }
                return scopeList;
        }
        //获取所有的地址
        public String getAddressName(){

            for(int i=0;i<date.size();i++){
                if(date.get(i).getCloseId()==1){
                    buff.append(date.get(i).getGiName()+",");
                }
            }
            buff.deleteCharAt(buff.length()-1);
            return buff.toString();
        }


        //初始化数据
        private void adapterInitView(final GeoInfoEntity geoInfoEntity, final MyHolder holder){
            holder.item_address_tv.setText(geoInfoEntity.getGiName());
            if(geoInfoEntity.getCloseId()==0){
                holder.item_address_cb.setChecked(false);
            }else{
                holder.item_address_cb.setChecked(true);
            }
            holder.item_address_cb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(geoInfoEntity.getCloseId()==0){
                        holder.item_address_cb.setChecked(true);
                        geoInfoEntity.setCloseId(1);
                    }else{
                        holder.item_address_cb.setChecked(false);
                        geoInfoEntity.setCloseId(0);
                    }
                }
            });

            holder.item_address_fl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(geoInfoEntity.getCloseId()==0){
                        holder.item_address_cb.setChecked(true);
                        geoInfoEntity.setCloseId(1);
                    }else{
                        holder.item_address_cb.setChecked(false);
                        geoInfoEntity.setCloseId(0);
                    }
                }
            });

        }

    }
class MyHolder {
    private TextView item_address_tv;
  private CheckBox item_address_cb;
    private FrameLayout item_address_fl;
    public MyHolder(View convertView){
        item_address_tv=(TextView)convertView.findViewById(R.id.item_address_tv);
        item_address_cb=(CheckBox)convertView.findViewById(R.id.item_address_cb);
        item_address_fl=(FrameLayout)convertView.findViewById(R.id.item_address_fl);


    }


}


}
